package controller.business.assessPool;

import base.response.OffsetLimitPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import controller.common.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import services.business.BusinessInfoServices;
import services.business.CompanyAssessServices;
import services.business.InspectorAssessServices;
import services.business.InspectorInfoServices;
import util.DateUtil;
import wt.entity.business.BusinessInfo;
import wt.entity.business.InspectorAssess;
import wt.entity.business.InspectorInfo;
import wt.staticData.ShortData;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller
@RequestMapping("business/assessPool")
public class AssessPoolController extends BaseController {
    @Autowired
    CompanyAssessServices assessPoolServices;
    @Autowired
    BusinessInfoServices businessInfoServices;
    @Autowired
    InspectorInfoServices inspectorInfoServices;
    @Autowired
    InspectorAssessServices inspectorAssessServices;


    @RequiresPermissions("business:assessPool:default")
    @RequestMapping("index")
    public String index(String assessType,Map map) {
        if (StringUtils.isBlank(assessType)) {
            assessType = "inspectorAssess";
        }
        map.put("assessType",assessType);
        return "business/assessPool/" + assessType;
    }


    @RequiresPermissions("business:assessPool:default")
    @ResponseBody
    @RequestMapping("select")
    public OffsetLimitPage select(
            String companyName,
            String productName,
            String fromPerson,
            String toPerson,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
            String assessType,
            Integer offset,
            Integer limit
    ) throws Exception {
        PageHelper.offsetPage(offset, limit);

        if ("inspectorAssess".equals(assessType)) {
            List<InspectorAssess> inspectorAssessList = inspectorAssessServices.selectInspectorAssessHistory(startTime, DateUtil.moveDate(endTime, 1), companyName, productName, fromPerson, null, toPerson);
            inspectorAssessServices.lazyLoadBusinessInfo(inspectorAssessList);
            Map<String, InspectorInfo> map = new HashMap<String, InspectorInfo>();
            for (InspectorAssess inspectorAssess : inspectorAssessList) {
                if (!map.containsKey(inspectorAssess.getToInspectorId())) {
                    map.put(inspectorAssess.getToInspectorId(), inspectorInfoServices.selectByPrimaryKey(inspectorAssess.getToInspectorId()));
                }
                inspectorAssess.setToInspectorInfo(map.get(inspectorAssess.getToInspectorId()));
                if (!map.containsKey(inspectorAssess.getFromInspectorId())) {
                    map.put(inspectorAssess.getFromInspectorId(), inspectorInfoServices.selectByPrimaryKey(inspectorAssess.getFromInspectorId()));
                }
                inspectorAssess.setFromInspectorInfo(map.get(inspectorAssess.getFromInspectorId()));
            }
            return new OffsetLimitPage((Page) inspectorAssessList);

        } else if ("companyAssess".equals(assessType)) {

            List<BusinessInfo> businessInfoList = businessInfoServices.select(null, null, toPerson, startTime, DateUtil.moveDate(endTime, 1), companyName, productName, null, null, null, ShortData.s1);
            return new OffsetLimitPage((Page) businessInfoList);

        } else if ("managerAssess".equals(assessType)) {

            List<BusinessInfo> businessInfoList = businessInfoServices.select(null, fromPerson, toPerson, startTime, DateUtil.moveDate(endTime, 1), companyName, productName, null, null, ShortData.s1, null);
            return new OffsetLimitPage((Page) businessInfoList);

        } else if ("observerAssess".equals(assessType)) {

            List<BusinessInfo> businessInfoList = businessInfoServices.select(fromPerson, null, toPerson, startTime, DateUtil.moveDate(endTime, 1), companyName, productName, null, ShortData.s1, null, null);
            return new OffsetLimitPage((Page) businessInfoList);

        } else if ("observerReport".equals(assessType)) {

            List<BusinessInfo> businessInfoList = businessInfoServices.select(fromPerson, null, toPerson, startTime, DateUtil.moveDate(endTime, 1), companyName, productName, ShortData.s1, null, null, null);
            return new OffsetLimitPage((Page) businessInfoList);

        } else {
            return new OffsetLimitPage();
        }
    }


}
